let http = require("http");
let fs = require("fs");
let path = require("path");
let server = http.createServer((req, res) => {
    res.writeHead(200,{"content-type":"text/html;charset=utf-8"});
    let requrl = req.url;
    let persons = fs.readFileSync(__dirname + "/persons.json", "utf8")
    let user = { username: "admin1", password: "12345" }
    if (requrl == "/login") {
        persons = JSON.parse(persons);
        let reg = persons.findIndex((item) => {
            return item.username == user.username && item.password == user.password;
        })
        if (reg == -1) {
            persons.push(user);//将user加进去
            persons = JSON.stringify(persons);//
            fs.writeFileSync(__dirname + "/persons.json", persons, { flag: "w" })
            res.end("登录成功");
        }
        else {
            res.end("登录失败");
        }
    } else if (requrl == "/register") {
        persons = JSON.parse(persons);
        let users = persons.find((item) => {
            return user.username == item.username && user.password == item.password
        })
        if (users) {
            res.end("注册成功");
        } else {
            res.end("注册失败");
        }
        
    }

})

//监听端口
server.listen(3000, "127.0.0.1", () => {
    console.log("启动")
})